package com.vhall.component.service.export.handler;

import com.alibaba.fastjson.JSONObject;
import com.vhall.component.entity.export.ExportEntity;
import com.vhall.component.entity.export.dto.AdminExportCommonQueryCondition;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.framework.common.model.Result;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

/**
 * @author yuanzh
 */
@Component
public interface BaseAdminExportHandler {

    /**
     * 获取文件名称
     *
     * @return
     */
    String getFileName();


    /**
     * 计算导出的数据条数
     *
     * @param entity 导出对象
     * @return 总条数
     */
    int count(ExportEntity entity);

    /**
     * 通过ribbon调用其他非依赖组件
     *
     * @param restTemplate 客户端template
     * @param url          地址
     * @param condition    条件
     * @return 返回请求结果
     */
    @SuppressWarnings("unchecked")
    default Result<Integer> countByPostForObject(RestTemplate restTemplate, String url, AdminExportCommonQueryCondition condition) {
        Result<Integer> result;
        if (Objects.isNull(condition)) {
            result = restTemplate.postForObject(url, new JSONObject(), Result.class);
        } else {
            result = restTemplate.postForObject(url, condition, Result.class);
        }
        if (Objects.isNull(result)) {
            throw new BusinessException(BizErrorCode.BIZ_OPERATION_FAIL);
        }
        return result;
    }
}
